﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Test_XNA
{
    class SpriteManager
    {
        public enum State
        { 
            RunLeft,
            RunRight,
            StandUp
        };

        public bool _globool = false;
        public List<Rectangle> _RunRight = new List<Rectangle>();
        public List<Rectangle> _RunLeft  = new List<Rectangle>();
        public List<Rectangle> _StandUp = new List<Rectangle>();
        public List<Rectangle> tmp;
        public State _state = State.RunLeft;
        private Texture2D _sprite;
        private Vector2 _spritepos = new Vector2(0,0);
        private IEnumerator<Rectangle> _it;

        public void LoadContent(ContentManager theContentManager, string theAssetName)
        {
            _sprite = theContentManager.Load<Texture2D>(theAssetName);
            _RunLeft.Add(new Rectangle(100,100,100,100));
            _RunLeft.Add(new Rectangle(200,100,100,100));
            _RunRight.Add(new Rectangle(100,0,100,100));
            _RunRight.Add(new Rectangle(200,0,100,100));
            _StandUp.Add(new Rectangle(0,0,100,100));
                        this.setState(State.RunLeft);
        }

        public Sprite getImage()
        {
            Sprite _image = new Sprite();
            _image.sprite = _sprite;
            _image.rec = _it.Current;
            return (_image);
        }
        public void nextFrame()
        {
            if (!_it.MoveNext())
            {
                _it = tmp.GetEnumerator();
                this.nextFrame();
            }
         }

        public State getState()
        {
            return _state;
        }

        public void setState(State _new)
        {
           if (_state != _new || _globool == false)
            {
                if (_new == State.RunLeft)
                {
                    tmp = _RunLeft;
                    _it = _RunLeft.GetEnumerator();
                }
                else if (_new == State.RunRight)
                {
                    tmp = _RunRight;
                    _it = _RunRight.GetEnumerator();
                }
                else if (_new == State.StandUp)
                {
                    tmp = _StandUp;
                    _it = _StandUp.GetEnumerator();
                }
                else
                   _it = null;
            }
           _globool = true;
            _state = _new;
            this.nextFrame();
        }
    }
}
